package com.lh_travel.lohas.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.ChangeBounds;
import android.transition.Scene;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;

import com.lh_travel.lohas.R;

import java.util.ArrayList;

/**
 * Created by aaron on 2018/6/15.
 * 可以设置两个layout,用show来控制显示的是哪个layout
 */
public abstract class SwitchRecylerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerHolder> {
    private Context mContext;
    private ArrayList<T> list;
    private int layout_fir;
    private int layout_sec;
    public boolean show = false;
    private RecyclerView mRecy;
    private boolean ani;
    private int lastPosition = -1;

    public static final int COMMENT_FIRST = 1;
    public static final int COMMENT_SECOND = 2;

    public SwitchRecylerAdapter(Context mContext, RecyclerView mRecy, ArrayList<T> list, int layout_fir, int layout_sec, boolean ani, boolean show) {
        this.mContext = mContext;
        this.list = list;
        this.layout_fir = layout_fir;
        this.layout_sec = layout_sec;
        this.mRecy = mRecy;
        this.ani = ani;
        this.show = show;
    }

    @Override
    public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = null;

        if (viewType == COMMENT_FIRST) {

            view = LayoutInflater.from(mContext).inflate(layout_fir, parent, false);
            return new FristViewHolder(mContext, view);
        } else if (viewType == COMMENT_SECOND) {

            view = LayoutInflater.from(mContext).inflate(layout_sec, parent, false);
            return new SecondViewHolder(mContext, view);
        } else {

            return null;
        }
    }

    @Override
    public void onBindViewHolder(BaseRecyclerHolder holder, int position) {

        if (holder instanceof FristViewHolder) {

            convert(holder, list.get(position), position, COMMENT_FIRST);

        } else if (holder instanceof SecondViewHolder) {

            convert(holder, list.get(position), position, COMMENT_SECOND);
        }

        if (ani) {
            setAnimation(holder.itemView, position);
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {

        if (show) {
            return COMMENT_SECOND;
        } else {
            return COMMENT_FIRST;
        }
    }

    public void switchLayout(boolean show) {

        this.show = show;

        mRecy.requestLayout();
    }

    public abstract void convert(BaseRecyclerHolder holder, T item, int position, int layoutType);

    //FristViewHolder，持有每个Item的的所有界面元素
    public static class FristViewHolder extends BaseRecyclerHolder {

        public FristViewHolder(Context context, View itemView) {
            super(context, itemView);
        }
    }

    //FristViewHolder，持有每个Item的的所有界面元素
    public static class SecondViewHolder extends BaseRecyclerHolder {

        public SecondViewHolder(Context context, View itemView) {
            super(context, itemView);
        }
    }

    public abstract void setAnimation(View viewToAnimate, int position);
}
